package com.szh.zx.javacorecorret.chap2.two;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

// 章节 2.2.2
// 类CyclicBarrier 具有屏障重置性格
public class CyclicBarrierTest2 {
	public static void main(String[] args) throws InterruptedException {
		CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

		Thread2 th1 = new Thread2(cyclicBarrier);
		th1.start();
		Thread.sleep(500);
		System.out.println(cyclicBarrier.getNumberWaiting());

		Thread2 th2 = new Thread2(cyclicBarrier);
		th2.start();
		Thread.sleep(500);
		System.out.println(cyclicBarrier.getNumberWaiting());

		Thread2 th3 = new Thread2(cyclicBarrier);
		th3.start();
		Thread.sleep(500);
		System.out.println(cyclicBarrier.getNumberWaiting());

		Thread2 th4 = new Thread2(cyclicBarrier);
		th4.start();
		Thread.sleep(500);
		System.out.println(cyclicBarrier.getNumberWaiting());
	}

}
class Thread2 extends Thread{
	private CyclicBarrier cbRef;

	public Thread2(CyclicBarrier cbRef) {

		this.cbRef = cbRef;
	}

	@Override
	public void run() {
		try {
			cbRef.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
	}
}